package cac.fenjian.resource.logMonitor.service;

import cac.common.CallInterFace.service.CallInterfaceService;
import cac.fenjian.resource.logMonitor.dao.ErpLogDao;
import cac.gkinterface.service.WorkService;
import com.alibaba.fastjson.JSON;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.logstash.logback.encoder.org.apache.commons.lang.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class ErpLogService {
    @Autowired
    private ErpLogDao erpLogDao;
    @Autowired
    private CallInterfaceService callInterfaceService;
    @Autowired
    private WorkService workService;

    public PageInfo querySendErpLog(int startPage,int pageSize,HashMap queryParam){
        PageHelper.startPage(startPage,pageSize);
        List<HashMap> data = erpLogDao.querySendErpLog(queryParam);
        PageInfo pageInfo = new PageInfo(data);
        return pageInfo;
    }

    public PageInfo queryBackLog(int startPage,int pageSize,HashMap queryParam){
        PageHelper.startPage(startPage,pageSize);
        List<HashMap> data = erpLogDao.queryBackLog(queryParam);
        PageInfo pageInfo = new PageInfo(data);
        return pageInfo;
    }

    public ResponseMsg updateMsg(HashMap map){
        ResponseMsg responseMsg =new ResponseMsg();
        int i=erpLogDao.updateMsg(map);
        if(i>0){
            responseMsg.setRetCode("200").setMessage("修改成功");
        }else {
            responseMsg.setMessage("修改失败").setRetCode("201");
        }
        return responseMsg;
    }

    public ResponseMsg deleteLog(List<HashMap> list){
        ResponseMsg responseMsg =new ResponseMsg();
        int sum = 0;
        for(HashMap map : list){
            int cnt = erpLogDao.deleteLog(map);
            sum += cnt;
        }
        responseMsg.setRetCode("200").setMessage("删除完毕，成功删除"+sum+"条数据");
        return responseMsg;
    }

    public PageInfo searchMsgToTime(int startPage,int pageSize,HashMap queryParam){
        PageHelper.startPage(startPage,pageSize);
        List<HashMap> data = erpLogDao.searchMsgToTime(queryParam);
        PageInfo pageInfo = new PageInfo(data);
        return pageInfo;
    }

    public ResponseMsg reSendLog(List<HashMap> list){
        ResponseMsg responseMsg = new ResponseMsg();
        HashMap issuedTest = new HashMap();
        String rstl = "";
        int error = 0,success = 0;
        for(HashMap map : list){
            try {
                HashMap data = erpLogDao.queryReSendLog(map);
                callInterfaceService.sendBData("sendBData", "两网传输-发送业务数据",
                        CallInterfaceService.encodeBData(JSON.toJSONString(JSON.parseObject((String)data.get("BData")))), "手动重推");
                issuedTest.put("BData", data.toString());
                issuedTest.put("type", "101-ERP-RCVMSG");
                issuedTest.put("msgId", data.get("msgId"));
                issuedTest.put("retCode", "1");
                issuedTest.put("result", "推送完成");
                issuedTest.put("remark", "ScheduledByHand");
                success++;
            } catch (Exception e) {
                e.printStackTrace();
                rstl = ExceptionUtils.getFullStackTrace(e);
                if(rstl.length()>1999){
                    rstl = rstl.substring(0, 1999);
                }
                issuedTest.put("result", rstl);
                issuedTest.put("retCode", "0");
                error++;
            } finally {
                workService.issuedTest(issuedTest);
            }
        }
        return responseMsg.setRetCode("200").setMessage("推送完毕，推送成功:"+success+"条，失败："+error+"条");
    }
}
